<template>
  <div>
    <el-card>
      <div class="border">
        <span style="font-size: 17px;font-weight: bold"> 我共享的文档</span>
      </div>
      <div class="border1">
        <el-form :inline="true">
          <el-form-item label="标题：">
            <el-input
              class="mr5"
              v-model="formInfo.keywords"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
<!--          <el-form-item label="审核状态：">-->
<!--            <el-select-->
<!--              class="mr5"-->
<!--              v-model="formInfo.status"-->
<!--              placeholder="请选择"-->
<!--            >-->
<!--              <el-option-->
<!--                v-for="item in documentStatus"-->
<!--                :key="item.keyword"-->
<!--                :label="item.label"-->
<!--                :value="item.keyword"-->
<!--              ></el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
          <el-form-item label="文档类型:">
            <el-select
              class="mr5"
              v-model="formInfo.categoryId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in categoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="年份：">
            <el-date-picker
              v-model="formInfo.year"
              type="year"
              placeholder="选择年"
              value-format="yyyy"
              :clearable="false"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">
              搜索
            </el-button>
            <el-button icon="el-icon-s-open" type="primary" @click="clear">
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-row style="text-align: center;padding:20px">
        <page-table
          :page-data="DocumentPageList"
          stripe
          border
          :loading="loading"
          style="width: 100%;"
          :do-get-page-data="getMyRelease"
        >
          <el-table-column width="100" align="center" type="index" label="序号">
          </el-table-column>
          <el-table-column
            align="center"
            prop="title"
            label="标题"
            width="500"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <a class="task_title_a" @click="examine(scope.row)">{{
                scope.row.title
              }}</a>
            </template>
          </el-table-column>
          <el-table-column
            prop="fileCount"
            align="center"
            label="文档数量"
          ></el-table-column>
          <el-table-column
            prop="categoryName"
            align="center"
            label="文档类型"
          ></el-table-column>

          <el-table-column
            align="center"
            label="审核状态"
            show-overflow-tooltip
            prop="status"
          >
            <template slot-scope="scope">
              <sz-badge
                :value="scope.row.status | keyText('DOCUMENT_STATUS')"
                :type="statusStyle(scope.row)"
              ></sz-badge>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="发送部门"
            show-overflow-tooltip
            prop="organizeName"
          ></el-table-column>
          <el-table-column
            align="center"
            show-overflow-tooltip
            width="170"
            prop="createTime"
            label="发送时间"
          ></el-table-column>
          <el-table-column
            width="220"
            fixed="right"
            align="center"
            label="管理"
          >
            <template slot-scope="scope">
              <el-tooltip content="查看" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-view"
                  @click="examine(scope.row)"
                ></el-button>
              </el-tooltip>
<!--              <el-tooltip content="审核记录" placement="top">-->
<!--                <el-button-->
<!--                  size="mini"-->
<!--                  icon="el-icon-document"-->
<!--                  @click="examineHistory(scope.row)"-->
<!--                ></el-button>-->
<!--              </el-tooltip>-->
              <el-tooltip content="编辑" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  @click="edit(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                v-if="scope.row.status === 'REJECT'"
                content="重新发起审核"
                placement="top"
              >
                <span @click="handleTooltip(scope.row)">
                  <user-select
                    style="display: inline-block;margin:0 10px"
                    :func-submit="userSubmit"
                    size="mini"
                    type=""
                    btnLabel=""
                    icon="el-icon-s-promotion"
                    :isHiddenInput="true"
                    @getUser="getUser"
                  ></user-select>
                </span>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  @click="delete2(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </page-table>
      </el-row>
    </el-card>
    <el-dialog
      @close="fail"
      title="编辑共享文档"
      width="40%"
      :visible.sync="editStatus"
    >
      <el-form ref="form" :rules="rules" :model="formInfo1" label-width="120px">
        <el-form-item label="共享标题：" prop="title">
          <el-input
            v-model="formInfo1.title"
            placeholder="共享标题"
            class="input_content"
          ></el-input>
        </el-form-item>
        <el-form-item label="文档类型" prop="categoryId">
          <el-select
            class="mr5"
            v-model="formInfo1.categoryId"
            placeholder="请选择"
            @change="selectWorkName"
            style="width: 300px"
          >
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人员：" prop="acceptUsers">
          <user-select
            :isMulSelect="true"
            placeholder="请选择接收人员"
            v-model="formInfo1.acceptUsers"
          ></user-select>
        </el-form-item>
        <el-form-item label="选择文件：" prop="files">
          <el-upload
            class="upload-demo"
            :action="imgHead"
            :headers="head"
            :on-success="handleSuccess"
            :on-preview="handlePreview"
            :on-error="handleError"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item style="padding-top: 20px">
          <el-button type="primary" @click="release">确定</el-button>
          <el-button @click="fail">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.documentStatus.unshift({ label: "全部", keyword: "" });
    this.getMyRelease();
    this.getCategoryTree();
    // this.getOri();
  },
  data() {
    return {
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        categoryId: [
          { required: true, message: "请选择文档类型", trigger: "blur" }
        ],
        acceptUsers: [
          { required: true, message: "请选择接收人员", trigger: "blur" }
        ],
        organizeId: [
          { required: true, message: "请输入部门", trigger: "blur" }
        ],
        files: [{ required: true, message: "请上传文件", trigger: "blur" }]
      },
      //上传配置
      imgHead: window.api.sysUploadUrl + "?path=docunment",
      head: {
        userRole: "ADMIN",
        token: window.sessionStorage.getItem("token")
      },
      categoryList: [], //文档类型列表
      // oriList: [], //部门列表
      fileList: [],
      editStatus: false,
      loading: false,
      value4: [],
      formInfo1: {
        id: "",
        title: "",
        categoryId: "",
        categoryName: "",
        organizeId: "",
        organizeName: "",
        acceptUsers: "",
        files: "",
        fileCount: "",
        status: "",
        module: "DOCUMENT"
      },
      formInfo: {
        pageIndex: "",
        pageSize: 10,
        keywords: "",
        myCreate: 0,
        categoryId: "",
        deleteFlag: 0,
        status: "",
        module: "DOCUMENT",
        year: String(new Date().getFullYear())
      },
      documentStatus: this.getDictionary("DOCUMENT_STATUS"),
      DocumentPageList: { records: [] },
      reexamineInfo: {
        id: "",
        auditUserId: "",
        auditUserName: "",
        status: "WAIT_HANDLE"
      }
    };
  },
  methods: {
    examineHistory(row) {
      this.$router.push({ name: "examineHistory", query: { id: row.id } });
    },
    handleError(err, file, fileList) {
      this.$notify.error({
        title: "错误",
        dangerouslyUseHTMLString: true,
        duration: 2000,
        message: `<p style="display: block;width: 260px; word-wrap: break-word">${
          JSON.parse(err.message).message
        }</p>`
      });
    },
    statusStyle(row) {
      let cls = "";
      switch (row.status) {
        case "COMPLETE":
          cls = "success";
          break;
        case "WAIT_HANDLE":
          cls = "warning";
          break;
        case "REJECT":
          cls = "danger";
          break;
      }
      return cls;
    },
    handleTooltip(row) {
      this.reexamineInfo.id = row.id;
    },
    userSubmit() {
      if (!this.reexamineInfo.auditUserId) {
        this.$message.error("请选择人员");
        return;
      }
      let tem = this.reexamineInfo;
      this.API.againDocument({ body: tem }).then(ret => {
        this.success("发送成功");
        this.getMyRelease();
      });
    },
    getUser(val) {
      this.reexamineInfo.auditUserId = val.origId;
      this.reexamineInfo.auditUserName = val.userNames;
    },
    Reexamine(row) {
      this.$confirm("确定重新发送审核", "提示", {})
        .then(() => {
          let tem = { id: row.id, status: "WAIT_HANDLE" };
          this.API.againDocument({ body: tem }).then(ret => {
            this.success("发送成功");
            this.getMyRelease();
          });
        })
        .catch(() => {});
    },
    //确认编辑
    release() {
      this.formInfo1.module = "DOCUMENT";
      let tem = { body: this.formInfo1 };
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.API.addOrUpdateDocument(tem).then(ret => {
            this.success("保存成功");
            this.editStatus = false;
            this.getMyRelease();
          });
        }
      });
    },
    //获取分类名称
    selectWorkName(id) {
      let obj = {};
      obj = this.categoryList.find(item => {
        return item.id === id;
      });
      this.formInfo1.categoryName = obj.name;
    },
    //删除
    delete2(row) {
      this.$confirm("确认删除当前文档?", "提示", {})
        .then(() => {
          let tem = {
            id: row.id
          };
          this.API.deleteLogicDocumentById(tem).then(ret => {
            this.success("删除成功");
            this.getMyRelease();
          });
        })
        .catch(() => {});
    },
    //编辑
    edit(row) {
      this.formInfo1.id = row.id;
      this.formInfo1.title = row.title;
      this.formInfo1.categoryId = row.categoryId;
      this.formInfo1.categoryName = row.categoryName;
      this.formInfo1.organizeId = row.organizeId;
      this.formInfo1.organizeName = row.organizeName;
      this.formInfo1.acceptUsers = row.selectUser;
      this.formInfo1.files = row.fileIds;
      this.formInfo1.fileCount = row.fileCount;
      this.formInfo1.status = row.status;
      this.editStatus = true;
      this.API.getDocumentById({ id: row.id }).then(ret => {
        ret.data.files.map(item => {
          this.fileList.push({
            name: item.fileName,
            response: { id: item.id }
          });
        });
      });
    },
    //查看
    examine(row) {
      this.$router.push({ name: "examine", query: { row: row.id } });
    },
    //获取我共享的文档
    getMyRelease(val) {
      let tem = Object.assign(this.formInfo, val);
      this.API.getDocumentPageList(tem).then(ret => {
        this.DocumentPageList = ret.data;
      });
    },
    //获取文档类型
    getCategoryTree() {
      this.API.getCategoryTree({ type: "DOCUMENT" }).then(res => {
        this.categoryList = res.data;
      });
    },
    //搜索
    search() {
      this.getMyRelease(1);
    },
    //重置
    clear() {
      this.formInfo = {
        pageIndex: 1,
        pageSize: 10,
        keywords: "",
        myCreate: 0,
        deleteFlag: 0,
        status: "",
        categoryId: "",
        module: "DOCUMENT",
        year: String(new Date().getFullYear())
      };
      this.value4 = [];
      this.getMyRelease(1);
    },
    //el-upload方法
    //上传成功
    handleSuccess(res, file, fileList) {
      this.formInfo1.files = "";
      let ids = [];
      fileList.map(item => {
        if (item.response && item.response.id) {
          ids.push(item.response.id);
        }
      });
      this.formInfo1.files = ids.join(",");
      this.formInfo1.fileCount = fileList.length;
    },
    handleRemove(file, fileList) {
      let arr = this.formInfo1.files.split(",");
      let index = arr.indexOf(file.response.id);
      arr.splice(index, 1);
      this.formInfo1.files = arr.join(",");
    },
    handlePreview(file) {
      this.openTabPage(
        window.api.preFile +
          "?url=" +
          window.api.secretFilePreview +
          file.response.id +
          "?token=" +
          window.sessionStorage.getItem("token")
      );
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    fail() {
      this.editStatus = false;
      this.fileList = [];
    }
  }
};
</script>
<style scoped>
/deep/ .el-card__body {
  padding: 0;
}

.border {
  padding: 19px 0 19px 20px;
  border-bottom: 1px solid rgba(224, 238, 249, 1);
}

.border1 {
  padding: 19px 0 0px 20px;
  border-bottom: 1px solid rgba(224, 238, 249, 1);
}
</style>
